package com.xl.ffmpeg;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.Toast;

import com.xl.ffmpeg.databinding.ActivityMainBinding;
import com.xl.ffmpeg.play.FFmpegPlay;
import com.xl.ffmpeg.play.OnPerpareListener;

public class MainActivity extends AppCompatActivity {
    FFmpegPlay play;

    private boolean isTouch;
    private int duration = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        play = new FFmpegPlay(new OnPerpareListener() {
            @Override
            public void onPrepare() {
                duration = play.getDuration();
                if (duration == -1) {
                    binding.linearlayout.setVisibility(View.INVISIBLE);
                } else {
                    binding.linearlayout.setVisibility(View.VISIBLE);
                }
                showMsg("准备成功");
                play.startPlay();
                runOnUiThread(() -> {
                    binding.tvTime.setText("00:00/" + getMinutes(duration) + ":" + getSeconds(duration));
                });
            }

            @Override
            public void start() {
                showMsg("开始播放");
            }

            @Override
            public void onError(String msg) {
                showMsg(msg);
            }

            @Override
            public void onProgress(int time) {
                if (!isTouch) {
                    runOnUiThread(() -> {
                        if (duration != 0) {
                            binding.seekBar.setProgress(time * 100 / duration);
                            binding.tvTime.setText(getMinutes(time) + ":" + getSeconds(time) + "/" + getMinutes(duration) + ":" + getSeconds(duration));
                        }
                    });
                }

            }

            @Override
            public void stop() {
                showMsg("播放停止");
            }

            @Override
            public void release() {
                showMsg("释放资源");
            }
        });

        String path = "data/data/com.xl.ffmpeg/baozha.mp4";
//        play.setDataSource(path);
        play.setDataSource("rtmp://58.200.131.2:1935/livetv/hunantv");
        play.setSurfaceHolder(binding.surfaceView);
        binding.btnStartPlay.setOnClickListener(view -> play.prepare());


        binding.seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    binding.tvTime.setText(getMinutes(progress * duration / 100) + ":" + getSeconds(progress * duration / 100) + "/" + getMinutes(duration) + ":" + getSeconds(duration));
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isTouch = true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int seekProgress = seekBar.getProgress();
                int playProgress = seekProgress * duration / 100;
                play.setProgress(playProgress);
                isTouch = false;
            }
        });

    }

    public void showMsg(String msg) {
        runOnUiThread(() -> Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show());
    }


    // 119 ---> 1.多一点点
    private String getMinutes(int duration) { // 给我一个duration，转换成xxx分钟
        int minutes = duration / 60;
        if (minutes <= 9) {
            return "0" + minutes;
        }
        return "" + minutes;
    }

    // 119 ---> 60 59
    private String getSeconds(int duration) { // 给我一个duration，转换成xxx秒
        int seconds = duration % 60;
        if (seconds <= 9) {
            return "0" + seconds;
        }
        return "" + seconds;
    }

}